package com.itboye.ihomebank.activity.login;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.NetPublicConstant;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.web.WebActivity;

import java.util.Observable;
import java.util.Observer;
/**
 * 注册第一步（填写手机号）
 *
 */
public class RegisterActivityOne extends BaseOtherActivity implements OnClickListener,Observer{
	View v_statusbar;
	ImageView close_icon;//返回图片
	TextView add_shap_title_tv;//标题
	EditText resster_one_tel;//手机号
	TextView register_one_ok;//下一步
	TextView chakan_xieyi;
	private String phone;
	private UserPresenter userPresenter;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon://返回
			finish();
			break;
		case R.id.register_one_ok://下一步
			phone=resster_one_tel.getText().toString();
			if(TextUtils.isEmpty(phone)){
				ByAlert.alert("手机号码不能为空");
				return;
			}
//			if(!isMobileNO(phone)){
//				T.showShort(RegisterActivityOne.this, "请输入正确的电话号码！");
//				return;
//			}
			showProgressDialog("正在获取验证码,请稍后",true);
			getyzm();
			
			break;
			case R.id.chakan_xieyi:
//				Intent intent=new Intent(this,XieYiActivity.class);
//				startActivity(intent);
				Intent	intent = new Intent(this, WebActivity.class);
				intent.putExtra(
						"url",NetPublicConstant.WEB_URL+ "/copyright/index");
				intent.putExtra("title", "用户协议");
				startActivity(intent);

				break;
			
		default:
			break;
		}
	}
	
	/**
	 * 获取验证码
	 */
	private void getyzm() {
		userPresenter = new UserPresenter(this);
		userPresenter.send(resster_one_tel.getText().toString(), "1");
	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity=handlerError(data);
		if(entity!=null){
			if(!entity.getCode().equals(0)){
				if(entity.getEventType()==UserPresenter.send_success){
					String bean=(String) entity.getData();
					if(bean!=null){
						ByAlert.alert("验证码发送成功");
						Intent intent=new Intent(RegisterActivityOne.this,RegisterActivityTwo.class);
						intent.putExtra("phone", resster_one_tel.getText().toString());
						intent.putExtra("yzm", bean);
						startActivity(intent);
						RegisterActivityOne.this.finish();
					}else{
						ByAlert.alert("失败");
					}
				}if(entity.getEventType() == UserPresenter.send_fail){
					ByAlert.alert("验证码发送失败");
				}
			}
		}
		try {
			closeProgressDialog();
		}catch (Exception e){

		}
		
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_register_one;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("请填写手机号码");
		chakan_xieyi.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下划线
		chakan_xieyi.getPaint().setColor(getResources().getColor(R.color.blue_bj));
		chakan_xieyi.getPaint().setAntiAlias(true);//抗锯齿
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}
	
}
